package com.hb.guli.service.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.hb.guli.service.edu.entity.Subject;
import com.hb.guli.service.edu.entity.excel.ExcelSubject;
import com.hb.guli.service.edu.entity.vo.SubjectVo;
import com.hb.guli.service.edu.listener.ExcelSubjectListener;
import com.hb.guli.service.edu.mapper.SubjectMapper;
import com.hb.guli.service.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author jack
 * @since 2023-06-30
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void batchImport(MultipartFile file) throws IOException {
            final ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(),
                    ExcelSubject.class,
                    new ExcelSubjectListener(baseMapper));
            if (file.getOriginalFilename().endsWith(".xls")) {
                read.excelType(ExcelTypeEnum.XLS)
                        .sheet().doRead();
            }else{
                read.excelType(ExcelTypeEnum.XLSX)
                        .sheet().doRead();
            }

    }

    @Override
    public List<SubjectVo> nestedList() {

        return baseMapper.selectNestedListByParentId("0");
    }
}
